package szzy

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/szzy"
    szzyReq "github.com/flipped-aurora/gin-vue-admin/server/model/szzy/request"
)

type StudentProfileService struct {}
// CreateStudentProfile 创建学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService) CreateStudentProfile(SP *szzy.StudentProfile) (err error) {
	err = global.GVA_DB.Create(SP).Error
	return err
}

// DeleteStudentProfile 删除学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService)DeleteStudentProfile(ID string) (err error) {
	err = global.GVA_DB.Delete(&szzy.StudentProfile{},"id = ?",ID).Error
	return err
}

// DeleteStudentProfileByIds 批量删除学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService)DeleteStudentProfileByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]szzy.StudentProfile{},"id in ?",IDs).Error
	return err
}

// UpdateStudentProfile 更新学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService)UpdateStudentProfile(SP szzy.StudentProfile) (err error) {
	err = global.GVA_DB.Model(&szzy.StudentProfile{}).Where("id = ?",SP.ID).Updates(&SP).Error
	return err
}

// GetStudentProfile 根据ID获取学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService)GetStudentProfile(ID string) (SP szzy.StudentProfile, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&SP).Error
	return
}

// GetStudentProfileInfoList 分页获取学生档记录
// Author [piexlmax](https://github.com/piexlmax)
func (SPService *StudentProfileService)GetStudentProfileInfoList(info szzyReq.StudentProfileSearch) (list []szzy.StudentProfile, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&szzy.StudentProfile{})
    var SPs []szzy.StudentProfile
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["student_id"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&SPs).Error
	return  SPs, total, err
}
func (SPService *StudentProfileService)GetStudentProfilePublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
